﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Workflows.Components.TextMining.Collapse
{
    public class ImpaciiExtOrgIDComparer:IAttributeComparer
    {
        #region Implementation of IAttributeComparer

        /// <summary>
        /// specify either use code snippet or class
        /// </summary>
        public AttributeComparerType ComparerType
        {
            get { return AttributeComparerType.ClassComparer; }
        }

        public SqlDbType FieldDbType
        {
            get { return SqlDbType.Int; }
            set {  }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value1"></param>
        /// <param name="value2"></param>
        /// <param name="penalty"></param>
        /// <param name="unevenDistTableName"></param>
        /// <returns></returns>
        public double Compare(object value1, object value2, double penalty, SqlConnection conn, string unevenDistTableName)
        {
            if (value1 == null || value1 == DBNull.Value ||
                value2 == null || value2 == DBNull.Value)
                return 0;

            int id1 = 0;
            bool canParse1 = false;
            canParse1 = int.TryParse(value1.ToString(), out id1);
            int id2 = 0;
            bool canParse2 = false;
            canParse2 = int.TryParse(value2.ToString(), out id2);
            if (!canParse1 || !canParse2)
                return 0;
            if (id1 == id2)
                return 1;
            int distance = Math.Abs(id1 - id2);
            if (distance < 5)
                return 0.95;
            else
                return penalty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="values"></param>
        /// <param name="unevenDistTableName"></param>
        /// <returns></returns>
        public object Merge(List<object> values, SqlConnection conn, string unevenDistTableName)
        {
            object mode = null;
            int maxFreq = 0;
            Dictionary<object, int> freqs = new Dictionary<object, int>();
            int postiveID = 0;
            int maxPostiveFreq = 0;
            Dictionary<int, int> postiveFreqs = new Dictionary<int, int>();
            foreach (object val in values)
            {
                if (freqs.ContainsKey(val))
                    freqs[val] = freqs[val] + 1;
                else
                    freqs.Add(val, 1);
                if (val != null && val.ToString().Length > 0)
                {
                    bool canParse = false;
                    int id = 0;
                    canParse = int.TryParse(val.ToString(), out id);
                    if(canParse && id>0)
                    {
                        if (postiveFreqs.ContainsKey(id))
                            postiveFreqs[id] = postiveFreqs[id] + 1;
                        else 
                            postiveFreqs.Add(id,1);
                    }
                }
            }
            if (postiveFreqs.Count > 0)
            {
                foreach(int id in postiveFreqs.Keys)
                {
                    if(postiveFreqs[id]>maxPostiveFreq)
                    {
                        maxPostiveFreq=postiveFreqs[id];
                        postiveID = id;
                    }
                }
                return postiveID;
            }
            else
            {
                foreach (object val in freqs.Keys)
                {
                    if (freqs[val] > maxFreq)
                    {
                        maxFreq = freqs[val];
                        mode = val;
                    }
                }
                return mode;
            }
        }

        #endregion
    }
}
